package com.glq1218.role.controller;

import com.glq1218.domain.*;
import com.glq1218.role.bo.AddRoleBO;
import com.glq1218.role.bo.QueryRoleBO;
import com.glq1218.role.bo.RoleBO;
import com.glq1218.role.bo.UpdateRoleBO;
import com.glq1218.role.dto.AddRoleDTO;
import com.glq1218.role.dto.QueryRoleDTO;
import com.glq1218.role.dto.RoleDTO;
import com.glq1218.role.dto.UpdateRoleDTO;
import com.glq1218.role.service.RoleService;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description 角色控制器
 * @since 2023/4/11 13:56
 */
@RestController
@RequestMapping("/api/role")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService roleService;

    @GetMapping
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<PaginationResult<RoleDTO>> queryRoleList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryRoleDTO queryRoleDTO) {
        QueryRoleBO queryRoleBO = BeanCopyUtils.copyBean(queryRoleDTO, QueryRoleBO.class);
        // 调用service获取角色信息
        PaginationResult<RoleBO> paginationResult = roleService.queryRoleList(paginationParam, sortParam, intervalStr, queryRoleBO);
        // 获取BO转换为DTO
        List<RoleDTO> roleDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<RoleBO> records = paginationResult.getRecords();
            roleDTOList = BeanCopyUtils.copyBeanList(records, RoleDTO.class);
        }
        return R.ok(PaginationResult
                .<RoleDTO>builder()
                .total(paginationResult.getTotal())
                .records(roleDTOList)
                .build()
        );
    }

    @GetMapping("/queryAllRole")
    public R<List<RoleDTO>> queryAllRole() {
        List<RoleBO> roleBOList = roleService.queryAllRole();
        List<RoleDTO> roleDTOList = BeanCopyUtils.copyBeanList(roleBOList, RoleDTO.class);
        return R.ok(roleDTOList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<RoleDTO> getRoleById(@PathVariable Serializable id) {
        RoleBO roleBO = roleService.getRoleById(id);
        if (roleBO == null) {
            return R.fail("不存在该id的信息");
        }
        RoleDTO roleDTO = BeanCopyUtils.copyBean(roleBO, RoleDTO.class);
        return R.ok(roleDTO);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('system:role:update')")
    public R<?> updateRole(@RequestBody UpdateRoleDTO updateRoleDTO) {
        UpdateRoleBO updateRoleBO = BeanCopyUtils.copyBean(updateRoleDTO, UpdateRoleBO.class);
        String result = roleService.updateRole(updateRoleBO);
        return R.ok(result);
    }

    @PostMapping
    public R<?> addRole(@RequestBody AddRoleDTO addRoleDTO) {
        AddRoleBO addRoleBO = BeanCopyUtils.copyBean(addRoleDTO, AddRoleBO.class);
        String result = roleService.addRole(addRoleBO);
        return R.ok(result);
    }

    @DeleteMapping
    public R<?> deleteRole(@RequestParam("idList") List<Long> idList) {
        return R.ok(roleService.removeByIds(idList));
    }
}
